﻿using System.Threading;
using System.Threading.Tasks;

namespace ESRI.ArcGIS.Client.Tasks.Async
{
    public static partial class GenerateRendererTaskExtensions
    {
        #region ExecuteTaskAsync

        /// <summary>
        /// Generates renderer based on renderer parameters.
        /// </summary>
        /// <param name="task"></param>
        /// <param name="generateRendererParameters">The renderer parameters.</param>
        /// <returns></returns>
        public static Task<GenerateRendererTaskResult> ExecuteTaskAsync(this GenerateRendererTask task,
            GenerateRendererParameters generateRendererParameters)
        {
            return ExecuteTaskAsync(task, generateRendererParameters, CancellationToken.None);
        }

        /// <summary>
        /// Generates renderer based on renderer parameters.
        /// </summary>
        /// <param name="task"></param>
        /// <param name="generateRendererParameters">The renderer parameters.</param>
        /// <param name="cancellationToken">Cancellation token for cancelling task.</param>
        /// <returns></returns>
        public static Task<GenerateRendererTaskResult> ExecuteTaskAsync(this GenerateRendererTask task,
            GenerateRendererParameters generateRendererParameters, CancellationToken cancellationToken)
        {
            return task.HandleTask<GenerateRendererTask, GenerateRendererResultEventArgs, GenerateRendererTaskResult>(
                t => t.ExecuteAsync(generateRendererParameters),
                (t, e) => t.ExecuteCompleted += e,
                (t, e) => t.ExecuteCompleted -= e,
                e => new GenerateRendererTaskResult(e),
                cancellationToken);
        }

        #endregion
    }
}


